<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/1/13
 * Time: 9:45
 */

namespace Upsanddowns\Controller;


//use Think\Controller;

class MyRecordController extends BaseController
{
    public function index(){
       
        $user=cookie('user_auth');
        $openid=$user['openid'];
        //$openid = "o6VUpxI3SRM2vaw9FJIMUmxjO2to";
        //根据openid获取用户信息
        $where_follow['openid']=array('eq',$openid);
        $where_follow['status']=array('eq',1);
        $follow_list=M('follow')->where($where_follow)->find();
        // var_dump($follow_list);
        $uid = $follow_list['id'];
        //获取当前期信息
        $periods = M('RoseActivity')->where('status=1')->order('periods_start_time desc')->getField('periods');
        //取出当前期数的所有活动id
        //$activites = M('RoseActivity')->where('periods='.$periods)->getField('id',true);
        //print_r($activites);
        //获取排名列表
        $where = array();
        $where['periods'] = $periods;
        //$where['rose_activity_id'] =array('in',$activites);
        $where['user_id']=array("eq",$uid);
        $rose_buy_list=M('RoseBuy')->where($where)->order('date desc')->select();
        foreach($rose_buy_list as $key=>$val){
            if($val['profit_gold_bean']==0){
                $carried_out_list[]=$val;
            }else{
                $end_list_all[]=$val;
            }
        }
       // var_dump($carried_out_list);
       // var_dump($end_list_all);
        $where_zhuijing['date']=array('eq',$end_list_all[0]['date']);
        $where_zhuijing['user_id']=array('eq',$uid);
        $where_zhuijing['profit_gold_bean']=array('neq',0);//验证在结束中
        $end_list=M('RoseBuy')->where($where_zhuijing)->select();
       // var_dump($end_list);
        $total_record = 0;//总战绩
        $total_gold_bean=0;//总投资
        foreach ($end_list as $data){
            $total_record = intval($total_record + $data['profit_gold_bean']);
            $total_gold_bean=intval($total_gold_bean+$data['gold_bean']);
        }
        $rate = sprintf("%.2f",($total_record/$total_gold_bean)*100);
		//echo $rate;
        //排列所有参与人员的排名
        $rankList = $this->getRankList();
       // $this->assign('rankList',$rankList);
        //取出我的排名
        $myRank = $this->getMyRank($rankList);
       //print_r($myRank);
        $this->assign('myRank',$myRank);
        //用户信息
        $this->assign('total_record',$total_record);
        $this->assign('rate',$rate);
        $this->assign('total_gold_bean',$total_gold_bean);
        $this->assign('carried_out_list',$this->time_day($carried_out_list));//进行中
        $this->assign('end_list', $this->time_day($end_list));//结束
        $this->assign('follow_list',$follow_list);//用户信息
        $this->display('index');
    }
    /**
     * [getMyRank 获取我当前期的总排名]
     * @return [type] [description]
     */
    private function getMyRank($rankList){
        $user = cookie('user_auth');
      // $user['uid']=408;
        $myRank = array();
        foreach($rankList as $rank=>$info){
            if($info['user_id'] == $user['uid']){
                $myRank = $info;
                $myRank['rank'] = $rank+1;
                break;
            }
            continue;
        }
        return $myRank;
    }

    /**
     * [getRankList 获取当前期所有排名明细]
     * @return [type] [description]
     */
    private function getRankList(){
        //获取当前期信息
        $periods = M('RoseActivity')->where('status=1')->order('periods_start_time desc')->getField('periods');
        //$periods = M('RoseActivity')->getField('max(periods) as periods');
        //取出当前期数的所有活动
        //$activites = M('RoseActivity')->where('periods='.$periods)->getField('id',true);
        //获取排名列表
        $where = array();
        $where['periods'] = $periods;
        $where['profit_gold_bean'] = array('neq',0);
        //$where['rose_activity_id'] =array('in',$activites);
        //$persons = M('RoseBuy')->distinct(true)->where($persons_where)->getField('user_id',true);
        $ranklist = M('RoseBuy')->field("user_id,sum(profit_gold_bean) as bean,count(*) as bits,min(date) as min_date,max(date) as max_date,max(add_time) as max_time")->where($where)->group('user_id')->order('bean desc,bits desc,min_date asc,max_date desc,max_time asc')->select();
        return $ranklist;
    }
    //时间处理
    private function time_day($carried_out_list){
        foreach($carried_out_list as $key=>$val){
            $add_time=$val['add_time'];
            $hi=date('H:i',$add_time);
            $carried_out_list[$key]['hi']=$hi;
            $y=date('Ymd')-date('Ymd',$add_time);
            if($y==0) {
                $carried_out_list[$key]['day'] = "今天";
            }else if($y==1){
                $carried_out_list[$key]['day']="昨天";
            }else if($y==2){
                $carried_out_list[$key]['day']="前天";
            }else{
                $carried_out_list[$key]['day']="过去";
            }
        }
		$carried_out_list=$this->array_sort($carried_out_list,'add_time','desc');
		/*$add_time_desc = array();
   		foreach ($carried_out_list as $val) {
       		$$add_time_desc[] = $val['add_time'];
   		}
   		array_multisort($add_time_desc, SORT_DESC, $carried_out_list);*/
        return $carried_out_list;
    }
	    /*
     * 个人战绩图
     * 根据时间
     */
    public function time_record_img(){
		$date=I('post.data');
        $time_status=array();//返回数据集合
        $user=cookie('user_auth');
        if(empty($date)){
            $date=date('Ymd');
        }
        $time_arr=$this->getthemonth($date);
        $where['date']  = array('between',array($time_arr[0],$time_arr[1]));
        $where['profit_gold_bean']=array('neq',0);
        $where['user_id']=$user['uid'];
        //$where['user_id'] = 417;
        $list=M('RoseBuy')->field('date,sum(profit_gold_bean) as profit_gold_bean')->where($where)->group('date')->select();
		$data=array();
        foreach($list as $k=>$v){
			$year=date('Y-m',strtotime($v['date']));
			$day=date('d',strtotime($v['date']));
            if($v['profit_gold_bean']>0){
                $status=0;
            }else{
               $status=1;
            }
			$data[]=array($year,$day,$status);
        }
		echo json_encode($data);exit;
    }
    /*
     * 获取一月中的开始一天和结束一天的时间
     */
    private function getthemonth($date)
    {
        $firstday = date('Ym01', strtotime($date));
        $lastday = date('Ymd', strtotime("$firstday +1 month -1 day"));
        return array($firstday, $lastday);
    }
	private function array_sort($array=array(),$keys,$type='asc'){
   		$keysvalue = $new_array = array();
   		//提取排序的列
   		foreach ($array as $k=>$v){
       		$keysvalue[$k] = $v[$keys];
   		}
   		if($type == 'asc'){
       		asort($keysvalue);//升序排列
   		}else{
      	 	arsort($keysvalue);//降序排列
   		}
   		//reset($keysvalue);
   		foreach ($keysvalue as $k=>$v){
       		$new_array[$k] = $array[$k];
   		}
   		return $new_array;
	}//排序比较时 要用数字；

}

